package controllers

import (
	"encoding/json"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"
	"gitchina/kly_service/util"

	"github.com/astaxie/beego"
)

type OrderController struct {
	beego.Controller
}

// @router /103001 [post]
func (o *OrderController) MakeWaybill() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		waybillRS, err := ord.MakeWaybill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(waybillRS)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103002 [post]
func (o *OrderController) UpdateWaybill() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		err := ord.UpdateWaybill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(nil)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103003 [post]
func (o *OrderController) GetWaybillList() {
	var rq models.GetWaybillListRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := ord.GetWaybillList(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetWaybillList] - debug:%v", rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103005 [post]
func (o *OrderController) GetWaybillDetail() {
	var rq models.GetWaybillDetailRQ
	var ord models.Order
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs, err := ord.GetWaybillDetail(rq.Id)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103006 [post]
func (o *OrderController) UpdateWaybillState() {
	var rq models.UpdateWaybillStateWithParamsRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.UpdateWaybillState(mbrId, &rq); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103007 [post]
func (o *OrderController) ViewContract() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		contract := ord.ViewContract(mbrId, &rq)
		o.Data["json"] = models.NewOkResponse(contract)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103008 [post]
func (o *OrderController) MakeLinebill() {
	var rq models.Linebill
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		id, err := ord.MakeLinebill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(id)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103009 [post]
func (o *OrderController) GetLinebillList() {
	var rq models.GetLinebillListRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := ord.GetLinebillList(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetLinebillList] - debug:%v", rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103010 [post]
func (o *OrderController) GetLinebillDetail() {
	var rq models.GetLinebillDetailRQ
	var ord models.Order
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs, err := ord.GetLinebillDetail(rq.Id)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103011 [post]
func (o *OrderController) UpdateLinebillState() {
	var rq models.UpdateLinebillStateRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		err := ord.UpdateLinebillState(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(nil)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103012 [post]
func (o *OrderController) ViewLetterOfAuthorization() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		contract := ord.ViewLetterOfAuthorization(rq.Id)
		o.Data["json"] = models.NewOkResponse(contract)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103013 [post]
func (o *OrderController) UpdateInvoice() {
	var ord models.Order
	var rq models.InvoiceInfo
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.UpdateInvoice(mbrId, models.ID(rq.WaybillId), rq); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103014 [post]
func (o *OrderController) GetInvoice() {
	var ord models.Order
	var rq models.InvoiceInfo
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if invoice, err := ord.GetInvoice(mbrId, models.ID(rq.WaybillId)); err == nil {
			o.Data["json"] = models.NewOkResponse(invoice)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103015 [post]
func (o *OrderController) GetDefaultOilCardNo() {
	var oil models.OilCard
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	no := oil.GetDefaultOildCardNo(mbrId)
	o.Data["json"] = models.NewOkResponse(no)
	o.ServeJSON()
}

// @router /103016 [post]
func (o *OrderController) UpdateOilCard() {
	var ord models.Order
	var rq models.UpdateOilCardRQ
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.UpdateOilCard(rq.WaybillId, mbrId, rq.NeedOilCard, rq.OilCardNo); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103017 [post]
func (o *OrderController) GetWaybillMessage() {
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	list := ord.GetWaybillMessage(mbrId)
	o.Data["json"] = models.NewOkResponse(list)
	o.ServeJSON()
}

// @router /103018 [post]
func (o *OrderController) AddWaybillScore() {
	var ord models.Order
	var rq models.AddWaybillScoreRQ
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.UpdateWaybillScore(rq.WaybillID, rq.Score, rq.Comment); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103019 [post]
func (o *OrderController) GetWaybillScore() {
	var ord models.Order
	var rq models.GetWaybillScoreRQ
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		o.Data["json"] = models.NewOkResponse(ord.GetWaybillScoreList(rq.CarrierOwnerID))
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103020 [post]
func (m *MemberController) GetOwnerScoreAndOrderCount() {
	var order models.Order
	carrierOwnerID := m.GetString("carrier_owner_id", "")
	m.Data["json"] = models.NewOkResponse(order.GetScoreAndOrderCount(models.ID(carrierOwnerID)))
	m.ServeJSON()
}

// @router /103021 [post]
func (o *OrderController) UpdateWaybillOfflineState() {
	var ord models.Order
	var rq models.UpdateOfflineWaybillStateWithParamsRQ
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.UpdateOfflineState(mbrId, &rq); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103022 [post]
func (o *OrderController) CompleteOfflineInvoice() {
	var ord models.Order
	var rq models.UpdateWaybillStateWithParamsRQ
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if err := ord.CompleteOfflineInvoice(mbrId, models.ID(rq.Id)); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}
